package cn.e3mall.content.service.impl;

import cn.e3mall.common.jedis.JedisClient;
import cn.e3mall.common.pojo.EasyUIDataGridResult;
import cn.e3mall.common.utils.E3Result;
import cn.e3mall.common.utils.JsonUtils;
import cn.e3mall.content.service.ContentService;
import cn.e3mall.mapper.TbContentMapper;
import cn.e3mall.pojo.TbContent;
import cn.e3mall.pojo.TbContentExample;
import cn.e3mall.pojo.TbContentExample.Criteria;
import cn.hutool.core.util.StrUtil;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * @description 内容列表管理的service接口实现
 * @author JiangBin
 * @date 2018年3月1日 下午9:10:40
 * @version 1.0.0 @
 */
@Service
public class ContentServiceImpl implements ContentService {

	@Autowired
	private TbContentMapper contentMapper;

	@Autowired
	private JedisClient jedisClient;

	@Value("${CONTENT_LIST}")
	private String CONTENT_LIST;

	@Override
	public E3Result addContent(TbContent content) {
		content.setCreated(new Date());
		content.setUpdated(new Date());
		// 插入数据
		contentMapper.insert(content);

		// 缓存同步
		jedisClient.hdel(CONTENT_LIST, content.getCategoryId().toString());

		return E3Result.ok();
	}

	@Override
	public List<TbContent> getContentListByCid(long cid) {
		// 1.先查询缓存是否有缓存数据
		try {
			String json = jedisClient.hget(CONTENT_LIST, cid + "");
			if (StrUtil.isNotEmpty(json)) {
				List<TbContent> list = JsonUtils.jsonToList(json, TbContent.class);
				return list;
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		// 设置条件
		TbContentExample example = new TbContentExample();
		Criteria criteria = example.createCriteria();
		criteria.andCategoryIdEqualTo(cid);
		// 查询
		List<TbContent> contents = contentMapper.selectByExampleWithBLOBs(example);
		// 将查询到数据进行缓存
		try {
			jedisClient.hset(CONTENT_LIST, cid + "", JsonUtils.objectToJson(contents));
		} catch (Exception e) {
			e.printStackTrace();
		}

		// 返回结果
		return contents;
	}

	@Override
	public E3Result deleteContent(String[] ids) {
		// 循环删除
		for (String id : ids) {
			contentMapper.deleteByPrimaryKey(Long.valueOf(id));
		}
		
		//缓存同步
		TbContent content = contentMapper.selectByPrimaryKey(Long.valueOf(ids[0]));
		Long categoryId = content.getCategoryId();
		jedisClient.hdel(CONTENT_LIST, categoryId+"");
		
		return E3Result.ok();
	}

	@Override
	public E3Result updateContent(TbContent content) {
		// 设置修改是时间
		content.setUpdated(new Date());
		contentMapper.updateByPrimaryKey(content);
		
		//缓存同步
		jedisClient.hdel(CONTENT_LIST, content.getCategoryId().toString());
		
		return E3Result.ok();
	}

	@Override
	public EasyUIDataGridResult getContentList(long cid, int page, int rows) {
		// 设置分页信息
		PageHelper.startPage(page, rows);
		// 设置查询条件执行查询
		TbContentExample example = new TbContentExample();
		Criteria criteria = example.createCriteria();
		criteria.andCategoryIdEqualTo(cid);
		List<TbContent> list = contentMapper.selectByExample(example);

		// 获取分页信息
		PageInfo<TbContent> pageInfo = new PageInfo<>(list);

		// 创建返回结果
		EasyUIDataGridResult result = new EasyUIDataGridResult();
		result.setTotal(pageInfo.getTotal());
		result.setRows(list);

		return result;
	}

}
